package com.ideaaedi.log4j2.defender.conerter;

import com.ideaaedi.log4j2.defender.defender.Log4j2MessageDefender;
import com.ideaaedi.log4j2.defender.util.DefenderUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;

/**
 * Log4j2MessageDefender转换器
 *
 * @author JustryDeng
 * @since 2021/7/23 0:24:09
 */
public class DefenderConverter implements Converter<String, Log4j2MessageDefender> {
    
    private final ApplicationContext applicationContext;
    
    public DefenderConverter(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Log4j2MessageDefender convert(String source) {
        try {
            return DefenderUtil.checkAndGetInstance(source, Log4j2MessageDefender.class, applicationContext);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            throw new IllegalArgumentException("Convert convert [" + source + "] to Log4j2MessageDefender.", e);
        }
    }
}
